﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Microsoft.Phone.Controls;
using MRadio.Model;

namespace MRadio
{
    public partial class Favorate : PhoneApplicationPage
    {
        private float Frenquency;

        public Favorate()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Favorate_Loaded);
        }

        void Favorate_Loaded(object sender, RoutedEventArgs e)
        {
            tbChannelName.SelectAll();
            Frenquency = float.Parse(this.NavigationContext.QueryString["F"].ToString());
            lbFavorite.ItemsSource =Global.ChannelConfig;
        }

        /// <summary>
        /// 新加收藏频道。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddChannel(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbChannelName.Text))
            {
              int result=Global.AddChannel(new FavoriteChannel() { ChannelName = tbChannelName.Text, Frequency = Frenquency });
              if (result == 1)
              {
                  MessageBox.Show(string.Format("The frequence {0}MHZ already in favorite list.", Frenquency));
              }
              else
              {
                  tbChannelName.Text = string.Empty;
                  lbFavorite.ItemsSource = Global.ChannelConfig;
              }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDeleteFavorite(object sender, RoutedEventArgs e)
        {            
            MenuItem item = sender as MenuItem;
            if (item != null)
            {
                Global.RemoveChannel(new FavoriteChannel() { Id = Guid.Parse(item.Tag.ToString()) });
                lbFavorite.ItemsSource = Global.ChannelConfig;
            }
        }

        /// <summary>
        /// 跳转到指定的频道。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSelectChanged(object sender, SelectionChangedEventArgs e)
        {
            FavoriteChannel obj=lbFavorite.SelectedItem as FavoriteChannel;
            if (obj != null && Global.ChangeFavoriteChanne!=null)
            {
                Global.ChangeFavoriteChanne(obj.Frequency);
            }
        }       
    }
}